#include <iostream>

#include "my_string.h"

using sympsel::string;

// 构造函数测试
void test1() {
    string s1;
    cout << s1 << endl;
    cout << s1.size() << endl;
    cout << s1.capacity() << endl;

    string s2("hello World1");
    cout << s2 << endl;
    cout << s2.size() << endl;
    cout << s2.capacity() << endl;

    string s3(s2);
    cout << s3 << endl;
    cout << s3.size() << endl;
    cout << s3.capacity() << endl;
}

void test2() {
    string s1("hello World2");
    for (char c : s1) {
        cout << c;
    }
    cout << endl;
    cout << s1.rbegin() << endl;
    // cout << s1.rend() << endl;
    for (auto it = s1.rbegin(); it != s1.rend(); --it) {
        cout << *it;
    }
    cout << endl;

    string s2("hello World2");
    cout << "input: ";
    std::cin >> s2;
    cout << s2 << endl;
}

// 运算符重载测试
void test3() {
    string s1("1111");
    string s2("22223333");
    s1 = s2;
    cout << s1 << endl;

    string s3("Hello World31");
    string s4("Hello World4");
    string s5;
    cout << (s3 > s4) << endl;
    cout << (s3 < s4) << endl;
    cout << (s3 >= s4) << endl;
    cout << (s3 <= s4) << endl;
    cout << (s3 == s4) << endl;
    cout << (s3 != s4) << endl;
    cout << (s3 <= s5) << endl;

    s5 += "123";
    s5 += "456";
    s5 += "1234567889";
    cout << s5 << endl;
}

// 查询测试
void test4() {
    string s1("hello World4");
    cout << s1.find('l') << endl;
    cout << s1.find('l', 3) << endl;
    if (s1.find("World") == string::npos) {
        cout << "not found" << endl;

    } else {
        cout << s1.find("World4") << endl;
    }

    if (s1.find("hell") == string::npos) {
        cout << "not found" << endl;

    } else {
        cout << s1.find("hell") << endl;
    }
}

// 插入测试
void test5() {
    string s1("hello World5");
    s1[2] = 'a';
    cout << s1 << endl;
    s1.insert(2, "bb");
    cout << s1 << endl;
    s1.insert(4, 'q');
    cout << s1 << endl;
    string s2("111");
    s1.insert(0, s2);
    cout << s1 << endl;
    s1.erase(2,2);
    cout << s1 << endl;
}

void test6() {
    string s1("helloc worldd");
    s1.erase(2,3);
    cout << s1 << endl;
    s1.erase(2, 10086);
    cout << s1 << endl;

    string s2("hello world");
    s2.erase(4);
    cout << s2 << endl;

    string s3("hello world");
    s3.erase(4, string::npos);
    cout << s3 << endl;
}

void test7() {
    string s1("hello world");
    s1 += '\0';
    s1 += "adsa";
    cout << s1 << endl;
    cout << s1.size() << endl;

    string s3("111");
    string s4("eeee");
    s3.swap(s4);
    cout << s3 << endl;
    cout << s4 << endl;

    string::swap(s3, s4);
    cout << s3 << endl;
    cout << s4 << endl;
}

// 字符串切割
void test8() {
    string s1("hello world,hello world,hello world");
    string s2 = s1.substr(0, s1.find(','));
    cout << s2 << endl;
}



int main() {
    void (*test[])() = {test1, test2, test3, test4, test5, test6, test7, test8};
    for (int i = 0; i < sizeof(test) / sizeof(*test); ++i) {
        cout << "=====" << "test" << i + 1 << "=====" << endl;
        test[i]();
    }
    return 0;
}
